package day10;

import org.junit.Test;

import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;


public class AppleTest {

    @Test
    public void testByColor(){
        List<Apple> datas = getApplies();
        if (datas == null) {
            System.out.println("未能获取到苹果数据");
            return;
        }

        System.out.println("总苹果数量: " + datas.size());

        //获得所有的红苹果
        List<Apple> redApples = FilterApple.filterByColor(datas,"红色");
        List<Apple> greenApples = FilterApple.filterByColor(datas,"绿色");

        if (redApples != null) {
            System.out.println("红色苹果数量: " + redApples.size());
        }
        if (greenApples != null) {
            System.out.println("绿色苹果数量: " + greenApples.size());
        }
    }

    private List<Apple> getApplies() {
        try {
            AppleSupplier supplier = new AppleSupplier();
            return supplier.getApplies();  // 调用AppleSupplier的getApplies方法获取实际数据
        } catch (Exception e) {
            System.err.println("获取苹果数据时出错: " + e.getMessage());
            return null;
        }
    }
    @Test
    public void testByWeight() {
        List<Apple> datas = getApplies();
        // 测试根据重量筛选，获取重量大于等于150的苹果
        List<Apple> heavyApples = FilterApple.filterByWeight(datas, 150.0);
        System.out.println("重量>=150的苹果数量: " + heavyApples.size());

        // 可以进一步验证结果
        for (Apple apple : heavyApples) {
            assert apple.getWeight() >= 150.0;
        }
    }

    @Test
    public void testByType() {
        List<Apple> datas = getApplies();
        System.out.println("总苹果数量: " + datas.size());

        // 打印前几个苹果的信息用于调试
        for (int i = 0; i < Math.min(5, datas.size()); i++) {
            Apple apple = datas.get(i);
            if (apple != null) {
                System.out.println("苹果 " + i + ": 品种=" + apple.getName() +
                        ", 颜色=" + apple.getColor() +
                        ", 重量=" + apple.getWeight());
            }
        }

        // 测试根据品种筛选，获取红富士苹果
        List<Apple> fujiApples = FilterApple.filterByType(datas, "红富士");
        System.out.println("红富士苹果数量: " + fujiApples.size());

        // 测试根据品种筛选，获取瑞雪苹果
        List<Apple> ruixueApples = FilterApple.filterByType(datas, "瑞雪");
        System.out.println("瑞雪苹果数量: " + ruixueApples.size());

        // 验证结果正确性
        for (Apple apple : fujiApples) {
            assert "红富士".equals(apple.getName());
        }
    }


    @Test
    public void testMapWeights() {
        List<Apple> datas = getApplies();
        if (datas == null) {
            System.out.println("未能获取到苹果数据");
            return;
        }

        // 使用 FilterApple.map 方法提取所有苹果的重量
        List<Double> weights = FilterApple.map(datas, Apple::getWeight);

        System.out.println("所有苹果的重量列表: " + weights);
    }


    //Consumer<T>
    @Test
    public void testMapWeights01() {
        List<Apple> datas = getApplies();
        if (datas == null) {
            System.out.println("未能获取到苹果数据");
            return;
        }

        System.out.println("所有苹果信息:");
        datas.forEach(apple -> System.out.println("品种: " + apple.getName() +
                ", 颜色: " + apple.getColor() +
                ", 重量: " + apple.getWeight()));

        // 使用 FilterApple.map 方法提取所有苹果的重量
        List<Double> weights = FilterApple.map(datas, Apple::getWeight);
        System.out.println("所有苹果的重量列表: " + weights);
    }


   // Supplier<T>
   @Test
   public void testAppleSupplier() {
       // 使用 Supplier 函数式接口创建 Apple 对象
       Supplier<Apple> s = () -> new Apple("瑞雪", "黄色", 134.5);
       Apple a = s.get();
       System.out.println("通过Supplier创建的苹果: " + a);

       // 验证创建的对象属性
       assert "红心".equals(a.getName()) : "苹果名称不匹配";
       assert "黄色".equals(a.getColor()) : "苹果颜色不匹配";
       assert a.getWeight() == 134.5 : "苹果重量不匹配";
   }


    @Test
    public void test(){
        Set<String> set=new HashSet<>();
        set.add("lucy");
        set.add("lily");
        set.add("lilei");
        set.add("lily");
        //[lilei, lily, lucy]
        System.out.println(set);

        //set的遍历
        for(String s:set){
            System.out.println(s);
        }

        //迭代器
        Iterator<String> iter = set.iterator();
        while(iter.hasNext()){
            String value=iter.next();
            System.out.println(value);
        }
    }


}
